<template>
    <el-container>
      <el-header>
        <el-menu
            default-active="1"
            class="el-menu-demo"
            mode="horizontal"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            :ellipsis="false"
        >
          <el-menu-item index="1" style="width: 200px">Dum - 在线商城</el-menu-item>
          <el-sub-menu index="2">
            <template #title>{{ admin.username }}</template>
            <el-menu-item index="center" @click="updateDialogShow = true">个人中心</el-menu-item>
            <el-menu-item index="chgPwd" @click="chgPwdDialogShow = true">修改密码</el-menu-item>
            <el-menu-item index="logout" @click="logout">退出</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
              active-text-color="#ffd04b"
              background-color="#545c64"
              class="el-menu-vertical-demo"
              text-color="#fff"
              :router="true">
            <el-menu-item index="/admin/index">
              <el-icon>
                <HomeFilled/>
              </el-icon>
              后台首页
            </el-menu-item>
            <el-sub-menu index="1">
              <template #title>
                <el-icon>
                  <Avatar/>
                </el-icon>
                <span>管理员管理</span>
              </template>
              <el-menu-item index="/admin/admin">管理员列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="2">
              <template #title>
                <el-icon>
                  <camera/>
                </el-icon>
                <span>商品分类</span>
              </template>
              <el-menu-item index="/admin/category">分类列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="3">
              <template #title>
                <el-icon>
                  <rank/>
                </el-icon>
                <span>商品管理</span>
              </template>
              <el-menu-item index="/admin/goods">商品列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="4">
              <template #title>
                <el-icon>
                  <UserFilled/>
                </el-icon>
                <span>用户管理</span>
              </template>
              <el-menu-item index="/admin/user">用户列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="5">
              <template #title>
                <el-icon>
                  <Ticket/>
                </el-icon>
                <span>订单管理</span>
              </template>
              <el-menu-item index="/admin/order">订单列表</el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main>
          <RouterView/>
        </el-main>
      </el-container>
    </el-container>

  <!--修改密码对话框开始 -->
  <el-dialog v-model="chgPwdDialogShow" title="添加商品" width="600">
    <el-form>

      <el-form-item label="密码" label-width="25%">
        <el-input v-model="chgPwdObj.oldPwd" placeholder="请输入密码" autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item label="新密码" label-width="25%">
        <el-input v-model="chgPwdObj.newPwd" placeholder="请输入新密码" autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item label="新密码确认" label-width="25%">
        <el-input v-model="chgPwdObj.newPwd1" placeholder="请确认新密码" autocomplete="off" style="width: 300px"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="chgPwdDialogShow = false">取消</el-button>
        <el-button type="primary" @click="chgPwd">确定</el-button>
      </div>
    </template>
  </el-dialog>
  <!--修改密码对话框结束-->
  <!--修改管理员信息对话框开始 -->
  <el-dialog v-model="updateDialogShow" title="修改管理员个人信息" width="600">
    <el-form>

      <el-form-item label="用户名" label-width="25%">
        <el-input v-model="admin.username" placeholder="" autocomplete="off" style="width: 300px" disabled/>
      </el-form-item>
      <el-form-item label="电话" label-width="25%">
        <el-input v-model="admin.phone" placeholder="请输入电话" autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item label="电子邮箱" label-width="25%">
        <el-input v-model="admin.email" placeholder="请输入电子邮箱" autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item label="真实姓名" label-width="25%">
        <el-input v-model="admin.realname" placeholder="请输入真实姓名" autocomplete="off" style="width: 300px"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="updateDialogShow = false">取消</el-button>
        <el-button type="primary" @click="chgInfo">确定</el-button>
      </div>
    </template>
  </el-dialog>
  <!--修改管理员信息对话框结束-->
</template>

<script setup>
import {RouterView} from 'vue-router'
import {ref} from 'vue';
import router from "@/router/index.js";
import {Avatar, Camera, HomeFilled, Rank, Ticket, UserFilled} from "@element-plus/icons-vue";
import {useTokenStore} from "@/stores/token.js";
import adminApi from "@/api/adminApi.js";
import {ElMessage} from "element-plus";

const tokenStore = useTokenStore();
const admin = ref({
  username: null,
  phone: null,
  email: null,
  realname: null
})
const chgPwdDialogShow = ref(false);
const updateDialogShow = ref(false);
let chgPwdObj = ref({
  oldPwd: null,
  newPwd: null,
  newPwd1: null
})

function logout() {
  tokenStore.$reset();
  router.push("/admin/login")
}

function getInfo() {
  adminApi.getInfo()
      .then(resp => {
        admin.value = resp.data;
      })
}

function chgInfo() {
  adminApi.chgInfo(admin.value)
      .then(resp => {
        if (resp.code == 10000) {
          ElMessage.success(resp.msg);
          updateDialogShow.value = false;
        } else {
          ElMessage.error(resp.msg)
        }
        getInfo();
      });
}

function chgPwd() {
  adminApi.chgPwd(chgPwdObj.value)
      .then(resp => {
        if (resp.code == 10000) {
          ElMessage.success(resp.msg);
          chgPwdObj.value = {
            oldPwd: null,
            newPwd: null,
            newPwd1: null
          }
          logout();
        } else {
          ElMessage.error(resp.msg);
        }
      });
}

getInfo()
</script>

<style scoped>
.el-menu--horizontal > .el-menu-item:nth-child(1) {
  margin-right: auto;
}

 .el-header {
  padding: 0px;

}

 .el-container, .el-menu {
  height: 100%;
}
</style>